package blackberry.util.field;

import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.component.ButtonField;

public class DjButtonField extends DjPreparedLabelManager {
	
	private ExtendedButtonField _button;

	public DjButtonField(DjLabelField itemLabel, DjLabelField infoLabel, String buttonName) {
		this(itemLabel, infoLabel, buttonName, DEFAULT_WIDTH);
	}

	public DjButtonField(DjLabelField itemLabel, DjLabelField infoLabel, String buttonName, 
			int width) {
		super(itemLabel, infoLabel, width);
		_button = new ExtendedButtonField(buttonName);
		add(_button);
	}

	/**
	 * @원리 height = button.height + infoLabel.height + padding
	 * 		 (infoLabel 은 실제 크기를 반환하기 위해 getHeight()를 사용함)
	 */
	public int getPreferredHeight() {
		return _button.getPreferredHeight() 
				+ _infoLabel.getHeight()
				+ DjFieldInfo.DEFAULT_PADDING_SIZE_px;
	}
	
	protected void sublayout(int width, int height) {
		
		setExtent(getPreferredWidth(), getPreferredHeight());
		
		setPositionChild(_itemLabel, 
				0, 
				DjFieldInfo.DEFAULT_PADDING_SIZE_px);
		layoutChild(_itemLabel, 
				_itemLabel.getPreferredWidth(), 
				_itemLabel.getPreferredHeight());
	
		setPositionChild(_button, 
				getPreferredWidth() - _button.getPreferredWidth(), 
				0);
		layoutChild(_button, 
				_button.getPreferredWidth(), 
				_button.getPreferredHeight());
		
		setPositionChild(_infoLabel, 
				0, 
				_button.getPreferredHeight());
		layoutChild(_infoLabel, 
				_infoLabel.getPreferredWidth(), 
				_infoLabel.getPreferredHeight());
	}
	
	
	/**
	 * button 필드의 대리 메서드들
	 */
	public void setFont(Font font) {
		_button.setFont(font);
	}
	
	public void setChangeListener(FieldChangeListener arg0) {
		_button.setChangeListener(arg0);
	}
	
	/**
	 * 확장해주지 않으면 버튼이 작아서 글자가 짤림
	 */
	static class ExtendedButtonField extends ButtonField {

		private static final int EXTENDED_WIDTH = Ui.convertSize(5, Ui.UNITS_mm, Ui.UNITS_px);
		private static final int EXTENDED_HEIGHT = Ui.convertSize(2, Ui.UNITS_mm, Ui.UNITS_px);

		public ExtendedButtonField(String label) {
			super(label);
		}
		
		public int getPreferredWidth() {
			return super.getPreferredWidth() + EXTENDED_WIDTH;
		}
		
		public int getPreferredHeight() {
			return super.getPreferredHeight() + EXTENDED_HEIGHT;
		}
		
		protected boolean invokeAction(int action) {
			return super.invokeAction(action);
		}
	}
}
